package org.example.flink.code;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import java.io.Serializable;
import java.util.Objects;

public class Hello07ReturnTypeUsePojo {
    public static void main(String[] args) throws Exception {
        //获取程序运行的环境
        StreamExecutionEnvironment environment =
                StreamExecutionEnvironment.getExecutionEnvironment();
        //调用Source方法创建DataStream
        DataStreamSource<String> lineStream =
                environment.socketTextStream("localhost", 19523);
        //转成User对象
        lineStream.map(word -> new User(word.split("-")[0], word.split("-")[1])).print();
        environment.execute();
    }
}
class User implements Serializable {
    private String uname;
    private String passwd;

//此处需要生成 [无参构造器][getter和setter][equals和hashcode][toString]
    public User() {}

    public User(String word, String number) {
        this.uname = word;
        this.passwd = number;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return Objects.equals(getUname(), user.getUname()) && Objects.equals(getPasswd(), user.getPasswd());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getUname(), getPasswd());
    }

    @Override
    public String toString() {
        return "User{" +
                "uname='" + uname + '\'' +
                ", passwd='" + passwd + '\'' +
                '}';
    }
}
